﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.BanBuon
{
    public partial class frmQuanLy : Form
    {
        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion

        #region Delegate form xử lý
        public void GetValue(Boolean capnhat)
        {
            try
            {
                XuLy();
                dgvHienThi.DataSource = hienthi;
                if (!capnhat)
                {
                    Them();
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Khởi tạo
        bool checkClose = false;
        Entities.HDBanBuon hdbanbuon;
        Entities.HDBanBuon[] hienthi;
        DateTime date;
        DateTime batdau;
        DateTime ketthuc;
        string makho = "";
        string tenkho = "";
        int i = 0;

        public frmQuanLy()
        {
            InitializeComponent();
            try
            {
                date = Entitystatic.date;
                batdau = ketthuc = date;
                Entities.CaiDatKho kho = Entitystatic.ViewFile();
                makho = kho.MaKho;
                tenkho = kho.TenKho;
                XuLy();
                dgvHienThi.DataSource = hienthi;
                fix();
            }
            catch
            {
            }
        }

        void XuLy()
        {
            hienthi = (Entities.HDBanBuon[])(Common.StaticVariable.InterfaceObject.SelectHDBanBuon().Where(p => p.MaKho == makho).ToArray());
        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.ReadOnly = true;
            dgvHienThi.Columns[Common.Constants.HDBanBuon.TongTienThanhToan].Visible = true;
            dgvHienThi.Columns[Common.Constants.HDBanBuon.MaHDBanBuon].Visible = true;
            dgvHienThi.Columns[Common.Constants.HDBanBuon.TenKhachHang].Visible = true;
            dgvHienThi.Columns[Common.Constants.HDBanBuon.TenKho].Visible = true;
            dgvHienThi.Columns[Common.Constants.HDBanBuon.NgayLap].Visible = true;
            dgvHienThi.Columns[Common.Constants.HDBanBuon.TongTienThanhToan].HeaderText = "Tổng Tiền";
            dgvHienThi.Columns[Common.Constants.HDBanBuon.MaHDBanBuon].HeaderText = "Mã Hóa Đơn";
            dgvHienThi.Columns[Common.Constants.HDBanBuon.TenKhachHang].HeaderText = "Tên Khách Hàng";
            dgvHienThi.Columns[Common.Constants.HDBanBuon.TenKho].HeaderText = "Tên Kho";
            dgvHienThi.Columns[Common.Constants.HDBanBuon.NgayLap].HeaderText = "Ngày Lập";
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHienThi.AllowUserToAddRows = false;
            dgvHienThi.AllowUserToDeleteRows = false;
            dgvHienThi.AllowUserToResizeRows = false;
            dgvHienThi.RowHeadersVisible = false;
        }
        #endregion

        #region Check Permission
        public bool CheckPer(int hanhdong)
        {
            return Common.Utilities.Check(Common.FormName.QuanLyBanBuon,hanhdong);
        }
        #endregion

        #region Thêm
        public void Them()
        {
            try
            {
                // check per
                if (!CheckPer(1))
                {
                    MessageBox.Show(Common.MSG.MSG10(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }

                frmXuLy frm = new frmXuLy();
                frm.GetData = new BanBuon.frmXuLy.GetString(GetValue);
                
                frm.Show();
            }
            catch
            {
            }
        }
        #endregion

        #region Sửa
        void Sua()
        {
            try
            {
                // check per
                if (!CheckPer(2))
                {
                    MessageBox.Show(Common.MSG.MSG9(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                if (i < 0)
                {
                    return;
                }
                if (dgvHienThi.RowCount == 0)
                {
                    return;
                }
                // check object
                hdbanbuon = ((Entities.HDBanBuon[])dgvHienThi.DataSource)[i];
                if (hdbanbuon == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                frmXuLy frm = new frmXuLy(hdbanbuon);
                frm.GetData = new BanBuon.frmXuLy.GetString(GetValue);
                frm.Show();
            }
            catch
            {
            }
        }
        #endregion

        #region Lọc Điều Kiện
        void LocDieuKien()
        {
            try
            {
                GUI.LocDieuKien.frmLocDieuKien frm = new LocDieuKien.frmLocDieuKien();
                frm.ShowDialog();
                if (GUI.LocDieuKien.frmLocDieuKien.kiemtra)
                {
                    DateTime truoc1;
                    DateTime sau1;
                    try
                    {
                        truoc1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.truoc);
                        sau1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.sau);
                    }
                    catch
                    {
                        return;
                    }
                    batdau = truoc1;
                    ketthuc = sau1;
                    XuLy();
                    dgvHienThi.DataSource = hienthi;
                    fix();
                    GUI.LocDieuKien.frmLocDieuKien.kiemtra = false;
                }
            }
            catch
            {
            }
        }
        #endregion

        #region In
        void In()
        {
            try
            {
                if (i < 0)
                {
                    return;
                }
                if (dgvHienThi.RowCount == 0)
                {
                    return;
                }
                // check object
                hdbanbuon = ((Entities.HDBanBuon[])dgvHienThi.DataSource)[i];
                if (hdbanbuon == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                this.Enabled = false;
                Entities.ChiTietHDBanBuon[] cthdbb =new frmXuLy().HienThiChiTiet(hdbanbuon);
                int sotang = 0;
                Entities.KhachHang kh = new Entities.KhachHang();
                kh.MaKhachHang = hdbanbuon.MaKhachHang;
                kh = Common.StaticVariable.InterfaceObject.Select_KhachHang(kh);
                Entities.BCHoaDonBanBuon[] bc = new Entities.BCHoaDonBanBuon[cthdbb.Length];
                foreach (Entities.ChiTietHDBanBuon item in cthdbb)
                {
                    bc[sotang] = new Entities.BCHoaDonBanBuon();
                    bc[sotang].ChietKhau = (double.Parse(item.ThanhTien) * (hdbanbuon.PhanTramChietKhauThuongmai) / 100);
                    bc[sotang].DonGia = Common.Utilities.FormatMoney(item.DonGia);
                    bc[sotang].DonVi = "";
                    bc[sotang].MaH = item.MaHangHoa;
                    bc[sotang].PTChietKhau = item.PhanTramChietKhau;
                    bc[sotang].SoLuong = item.SoLuong.ToString();
                    bc[sotang].STT = sotang + 1;
                    bc[sotang].TenH = item.TenHangHoa;
                    bc[sotang].ThanhTien = double.Parse(item.ThanhTien);
                    bc[sotang].ThueGTGT = item.ThueGTGT;
                    sotang++;
                }

                BaoCao.frmBaoCaorpt frm = new BaoCao.frmBaoCaorpt(bc, kh, hdbanbuon);
                if (frm.kiemtra)
                {
                    frm.ShowDialog();

                }
                this.Enabled = true;
            }
            catch
            {
            }
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            this.Close();
        }
        #endregion

        #region Tìm kiếm
        void TimKiem()
        {
            dgvHienThi.DataSource = (Entities.HDBanBuon[])hienthi.Where(p => p.MaHDBanBuon.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0).ToArray();
            fix();
        }
        #endregion

        #region Event
        private void frmQuanLy_Load(object sender, EventArgs e)
        {
            lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap;
            this.FormClosing +=new FormClosingEventHandler(frmQuanLy_FormClosing);
            btnIn.Click += new EventHandler(btnIn_Click);
            btnLocDieuKien.Click += new EventHandler(btnLocDieuKien_Click);
            btnSua.Click +=new EventHandler(btnSua_Click);
            btnThem.Click +=new EventHandler(btnThem_Click);
            btnThoat.Click +=new EventHandler(btnThoat_Click);
            btnTim.Click += new EventHandler(btnTim_Click);
            txtTimKiem.TextChanged += new EventHandler(txtTimKiem_TextChanged);
            dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvHienThi_CellClick);
            dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);
            dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown);
            this.WindowState = FormWindowState.Maximized;
        }

        void dgvHienThi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                Sua();
            }
        }

        void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Sua();
        }

        void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
        }

        void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            TimKiem();
        }

        void btnTim_Click(object sender, EventArgs e)
        {
            TimKiem();
        }

        void btnLocDieuKien_Click(object sender, EventArgs e)
        {
            LocDieuKien();
        }

        void btnIn_Click(object sender, EventArgs e)
        {
            In();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Them();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            Sua();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        private void frmQuanLy_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
        }
        #endregion
    }
}
#endregion